package mamikeji.liaowa.ui.activity.right;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.EventListBean;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.ui.adapter.EventListAdapter;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MultiStateView;
import mamikeji.liaowa.widget.refreshView.PtrlDownRefreshFrameLayout;

public class UserEventActivity extends BaseActivity {
    private static final String TAG = "UserEventActivity";
    private ListView listView_activity_date;
    private PtrlDownRefreshFrameLayout ptrlDownRefreshFrameLayout_activity_date;
    private EventListAdapter eventListAdapter;
    List<EventListBean.DataEntity> dataEntityList = new ArrayList<>();

    private MultiStateView multiStateView_activity_date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_event);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.user_event_name);
        init();
        initData();
        getEventList();
    }

    /**
     * 加载布局控件
     */
    private void init() {
        ptrlDownRefreshFrameLayout_activity_date = (PtrlDownRefreshFrameLayout) findViewById(R.id.ptrlDownRefreshFrameLayout_activity_date);
        listView_activity_date = (ListView) findViewById(R.id.listView_activity_date);
        eventListAdapter = new EventListAdapter(dataEntityList, this);
        listView_activity_date.setAdapter(eventListAdapter);

        eventListAdapter.setList(dataEntityList);

        multiStateView_activity_date = (MultiStateView) findViewById(R.id.multiStateView_activity_date);

        multiStateView_activity_date.getView(MultiStateView.VIEW_STATE_ERROR).findViewById(R.id.button_error_refish)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        multiStateView_activity_date.setViewState(MultiStateView.VIEW_STATE_LOADING);
                        getEventList();
                    }
                });
    }

    /**
     * 初始化数据
     */
    private void initData() {
        ptrlDownRefreshFrameLayout_activity_date.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, listView_activity_date, header);
            }
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getEventList();
            }
        });
        listView_activity_date.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(UserEventActivity.this, EventDetailsActivity.class);
                intent.putExtra("event_weburl", dataEntityList.get(i).getJumpurl() + "");
                startActivity(intent);
            }
        });
    }

    public void getEventList() {
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.EVENT_MY_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.EVENT_MY_datetime, "");
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.GET_MYACTIVITY, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                multiStateView_activity_date.setViewState(MultiStateView.VIEW_STATE_NOINTERNET);
                ptrlDownRefreshFrameLayout_activity_date.refreshComplete();
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                ptrlDownRefreshFrameLayout_activity_date.refreshComplete();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        EventListBean eventListBean = new Gson().fromJson(response, EventListBean.class);
                        if (eventListBean.getData() != null) {
                            dataEntityList = eventListBean.getData();
                            eventListAdapter.setList(dataEntityList);
                            multiStateView_activity_date.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {
                            multiStateView_activity_date.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                        }
                    } else {
                        multiStateView_activity_date.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}